package controllers

import (
	"news/common/response"
	"news/constStatus"
	"news/logic"
	"news/models"
	"strconv"

	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"

	"github.com/gin-gonic/gin"
)

//创建帖子部份

func CreatePostHandler(c *gin.Context) {
	p := new(models.Post)
	if err := c.ShouldBindJSON(p); err != nil {
		zap.L().Error("add post with invalid param", zap.Error(err))
		errors, ok := err.(validator.ValidationErrors)
		if !ok {
			response.FailWithMessage(err.Error(), c)
			return
		}
		response.FailWithMessage(removeTopStruct(errors.Translate(trans)), c)
		return
	}
	//业务逻辑处理
	AuthorId, _, _ := GetCurrentUserInfo(c)
	p.AuthorId = AuthorId
	err := logic.CreatePost(p)
	if err != nil {
		zap.L().Error("CreatePost(p) failed", zap.Error(err))
		response.FailWithMessage(constStatus.CodeServerBuy, c)
		return
	}
	response.OkWithMessage("success", nil, c)
	return
}

// 社区与post的关联关系数据
func PostListHandler(c *gin.Context) {
	communityList, err := logic.GetCommunityInfo()
	if err != nil {
		zap.L().Error("mysql.GetCommunityInfo() failed", zap.Error(err))
		response.ResponseErr(c, response.CodeServerBusy)
		return
	}
	response.ResponseSuccess(c, communityList)
}

// 帖子详情
func PostDetailHandler(c *gin.Context) {
	idStr := c.Param("id")
	pid, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		zap.L().Error("get post detail with invalid failed", zap.Error(err))
		response.FailWithMessage(constStatus.CodeParamError, c)
		return
	}
	data, err := logic.GetPostById(pid)
	if err != nil {
		response.FailWithMessage(constStatus.CodeServerBuy, c)
		return
	}
	communityList, err := logic.GetCommunityList()
	if err != nil {
		response.FailWithMessage(constStatus.CodeServerBuy, c)
		return
	}
	var m = make(map[string]interface{}, 10)
	m["communityList"] = communityList
	m["data"] = data
	response.OkWithMessage("success", m, c)
}

// 展示所有的帖子列表
func PostAllHandler(c *gin.Context) {
	p := new(models.Post)
	page, size := getPageInfo(c)
	dataInfo, err := logic.GetPostAll(p, page, size)
	if err != nil {
		zap.L().Error("logic.GetPostAll(p) failed", zap.Error(err))
		response.FailWithMessage(constStatus.CodeServerBuy, c)
		return
	}
	response.OkWithMessage("success", dataInfo, c)
}

// 根据前端传来的参数动态的获取帖子列表
// 按创建时间排序 或者 按照 分数排序
// 1、获取请求的query string 参数
// 2、去redis查询id列表
// 3、根据id去数据库查询帖子详细信息
// PostsIndexListHandler 升级版帖子列表接口
// @Summary 升级版帖子列表接口
// @Description 可按社区按时间或分数排序查询帖子列表接口
// @Tags 帖子相关接口
// @Accept application/json
// @Produce application/json
// @Param Authorization header string true "Bearer 用户令牌"
// @Param object query models.ParamPostIndexListForm false "查询参数"
// @Security ApiKeyAuth
// @Success 200 {object} _ResponsePostList "列表的数据查询 postsIndexList"
// @Router /postsIndexList [get]
func PostsIndexListHandler(c *gin.Context) {
	p := &models.ParamPostIndexListForm{
		Page:  1,
		Size:  10,
		Order: models.OrderTime,
	}
	if err := c.ShouldBindQuery(p); err != nil {
		zap.L().Error("PostsIndexListHandler with invalid param", zap.Error(err))
		response.ResponseErr(c, response.CodeInvalidParam)
		return
	}
	data, err := logic.GetPostListNew(p)
	if err != nil {
		response.ResponseErr(c, response.CodeServerBusy)
		return
	}
	response.ResponseSuccess(c, data)
}
